package com.gtja.beoms.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gtja.beoms.base.CommonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @ClassName: JsonConvertUtil
 * @Description: 共同json转换utils
 * @Author: Li
 * @Date: 2021.06.17
 * @Version: 1.0
 **/
public class JsonConvertUtil {
    private static Logger logger = LoggerFactory.getLogger(JsonConvertUtil.class);

    /**
     * @Description: json转换实体
     * @Author: Li
     * @Param: [json, target]
     * @Date: 2021/6/17 17:39
     * @returns: java.lang.Object
     */
    public static Object convert(String json, Class<?> target) throws Exception {
        return JSONObject.parseObject(json, target);
    }

    /**
     * @Description: 将json中的map抽取出来转换成javaObject
     * @Author: Li
     * @Param: [json, target]
     * @Date: 2021/6/17 17:39
     * @returns: java.lang.Object
     */
    public static Object convertResponseData(String json, Class<?> target) throws Exception {
        logger.info("入参json：[{}]", json);
        CommonResponse commonResponse = JSONObject.parseObject(json, CommonResponse.class);
        logger.info("转化得到的CommonResponse：[{}]", commonResponse);
        Map<String, Object> map = (Map<String, Object>) commonResponse.getData();
        logger.info("CommonResponse中的map为：[{}]", map);
        String data = JSON.toJSONString(map);
        logger.info("map转化为json为：[{}]", json);
        return JSONObject.parseObject(data, target);
    }

}
